﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.UI;
using System.Xml.Linq;
using WebMVC.Models;

namespace WebMVC.Controllers
{
    /// <summary>
    /// 学生信息管理
    /// </summary>
    public class StudentController : Controller
    {
        /// <summary>
        /// 学生信息分页显示
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 分页查询学生信息
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示几条</param>
        /// <returns></returns>
        public JsonResult GetStudentList(int pageIndex,int pageSize)
        {
            int s_num = (pageIndex-1)* pageSize+1;
            int e_num = pageIndex*pageSize;

            //写分页查询的SQL语句【注意每行字符串有前与后加空格】
            string sql = "";
            sql += "   select id, Name, Sex, Phone, Birthday, Age, Email, Pwd, CityId, Hobby, IsStart, HeaderImg, Num from  ";
            sql += "  (  ";
            sql += "  select id, Name, Sex, Phone, Birthday, Age, Email, Pwd, CityId, Hobby, IsStart, HeaderImg, ROW_NUMBER() over(order by id) as 'Num' from Student ";
            sql += $" ) temp where Num between {s_num} and {e_num} ";
            //获取分页学生记录
            List<StudentEntity>  students =  DBHelper.GetList<StudentEntity>(sql);

            //获取总记录数量
            string sql_count = " select count(1) from Student ";
            int count = Convert.ToInt32(DBHelper.Scalar(sql_count));

            return Json(new { Data= students,Count= count },JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加学生信息显示
        /// </summary>
        /// <returns></returns>
        public ActionResult AddStudent()
        {
            return View();
        }
        /// <summary>
        /// 保存学生信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveStudent(StudentEntity entity)
        {
            string sql = "  ";
            sql += "   insert into Student                                                                                           ";
            sql += "   (Name, Age, Email, Pwd, Sex, CityId, Birthday, Phone, Hobby, IsStart, HeaderImg)                              ";
            sql += "       values                                                                                                    ";
            sql += $"       ('{entity.Name}', 12, 'as@as.com', '123', 'True', 1, '2023-1-1', '13812345678', '游泳,编程', 'True', '{entity.HeaderImg}')   ";

            int count = DBHelper.Excute(sql);
            return Json(count);
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpFile()
        {
            string fileName = "";
            try
            {
                //获取文件
                HttpPostedFileBase file = Request.Files["abc"];
                //找出文件夹的在硬盘的具体位置
                string path = Server.MapPath("/Upfilse/") + file.FileName;
                //保存文件
                file.SaveAs(path);

                fileName = "/Upfilse/" + file.FileName;
            }
            catch (Exception)
            {
                throw;
            }
            return Json(fileName);
        }
    }
}